package com.bonc.common.utils;

import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * @description 连接服务器
 * @date 2020/9/12 21:40
 */

@Configuration
public class SftpConnectConfig {

    /** FTP 登录用户名*/
    @Value("${iosServer.username}")
    private String username;
    /** FTP 登录密码*/

    @Value("${iosServer.password}")
    private String password;
    /** FTP 服务器地址IP地址*/

    @Value("${iosServer.host}")
    private String host;
    /** FTP 端口*/

    @Value("${iosServer.port}")
    private String strPort;
    /** FTP 路径*/

    /*@Value("${iosServer.rootUrl}")
    private String rootUrl;*/

    private Session getSession() throws JSchException, NoSuchFieldException, IllegalAccessException, SftpException {
        JSch jsch = new JSch();
        /*int port = Integer.parseInt(strPort.trim());
        Session session = jsch.getSession(username, host, port);
        if (password != null) {
            session.setPassword(password);
        }*/

        int port = Integer.parseInt("1161");
        Session session = jsch.getSession("sfcsv3511", "10.254.30.252", port);
        session.setPassword("Sfcsv_351!");

        /*int port = Integer.parseInt("22");
        Session session = jsch.getSession("bonc", "192.168.100.202", port);
        session.setPassword("bonc123");*/

        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        session.setConfig(sshConfig);
        session.connect();

        return session;
    }

    private Session getSession1() throws JSchException {
        JSch jsch = new JSch();
        Session session = jsch.getSession("cusguoxinsys", "10.211.72.239", 22);
        session.setPassword("123qwe!@#");
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");

        session.setConfig(config);
        session.connect();
        return session;
    }

    /**
     * 连接sftp服务器,返回的是sftp连接通道，用来操纵文件
     * @throws Exception
     */

    @Bean(name = "channelSftp")
    public ChannelSftp channelSftp() {
        ChannelSftp sftp = null;
        try {
            Session session = getSession();
            ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
            channel.connect();
            Class cl = ChannelSftp.class;
            Field f =cl.getDeclaredField("server_version");
            f.setAccessible(true);
            f.set(channel, 2);
            channel.setFilenameEncoding("utf-8");

            sftp = channel;
        } catch (JSchException | NoSuchFieldException | IllegalAccessException | SftpException e) {
            System.out.println("连接失败");
        }
        return sftp;
    }

    @Bean(name = "channelSftp1")
    public ChannelSftp channelSftp1() {
        ChannelSftp sftp = null;
        try {
            Session session = getSession1();
            //Session session = getSession();
            ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
            channel.connect();
            Class cl = ChannelSftp.class;
            Field f =cl.getDeclaredField("server_version");
            f.setAccessible(true);
            f.set(channel, 2);
            channel.setFilenameEncoding("utf-8");
            sftp = channel;
        } catch (JSchException e) {
            System.out.println("连接失败");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return sftp;
    }

/**
 * 连接sftp服务器，返回exec连接通道，可以远程执行命令
 * @throws Exception
 */

   /* @Bean
    public ChannelExec channelExec(){
        ChannelExec sftp = null;
        try {
            Session session = getSession();
            Channel channel = null;
            channel = session.openChannel("exec");
            channel.connect();
            sftp = (ChannelExec) channel;
        } catch (JSchException e) {
            System.out.println("连接失败");
        }
        return sftp;
    }*/
}


